//
//  HLHomeController.swift
//  weibo-snh
//
//  Created by 黄力 on 15/12/14.
//  Copyright © 2015年 HLTest. All rights reserved.
//

import UIKit
import AFNetworking
import SnapKit
//重用标示符
private let homeCellID = "homeCell"

class HLHomeController: HLBaseController {
    
    //定义模型数组保存数组
    var homeStatusesArr: [HLHomeStatuses] = [HLHomeStatuses]()

    override func viewDidLoad() {
        super.viewDidLoad()
        //判断用户是否登录
        if HLUserAccountViewModel().loginStatus {
            setupUI()
        }else {
            visitorLoginView?.setVisitorLoginView("", imageName: nil)
        }
        
    }
    
    //设置界面
    private  func setupUI() {
        //注册cell
        tableView.registerClass(HLHomeCell.self, forCellReuseIdentifier: homeCellID)
        //设置预估行高,尽量接近真实行高,提高性能
        tableView.estimatedRowHeight = 300
        //设置根据内容自动计算行高
        tableView.autoresizesSubviews = true
        //设置分割线样式
        tableView.separatorStyle = .None
        //设置底部视图
        tableView.tableFooterView = footerIndicatorView
        //添加顶部视图
        tableView.addSubview(headerRefreshView)
        //添加响应事件
        headerRefreshView.addTarget(self, action: "loadHomeData", forControlEvents: .ValueChanged)
        //获取数据
        loadHomeData()
    }
    
    @objc private func loadHomeData() {
        //定义变量
        var since_id: Int64 = 0
        var max_id : Int64 = 0
        
        //判断当底部指示按钮正在动画中时传入参数
        //根据当前状态头部视图是不是刷新状态
        if footerIndicatorView.isAnimating() {
            //获去最后一个数据
            let lastStatus = homeStatusesArr.last
            max_id = lastStatus?.id ?? 0
        }else if headerRefreshView.status == .Refreshing {
            //获取第一条数据
            let firstStatus = homeStatusesArr.first
            since_id = firstStatus?.id ?? 0
        }
        
        HLHomeStatusesViewModel.loadHomeStatuses(since_id, max_id: max_id) { (statuses) -> () in
            //判断请求的模型数组是否有值
            guard let statusesList = statuses else {
                return
            }
            print("更新\(statusesList.count)条数据")
            //获取模型数组
            if since_id != 0 {
                //拼接数组
                self.homeStatusesArr = statusesList + self.homeStatusesArr
                //结束刷新
                self.headerRefreshView.endRefreshing()
            }else if max_id != 0 {
                //拼接数组
                self.homeStatusesArr = self.homeStatusesArr + statusesList
                //结束动画
                self.footerIndicatorView.stopAnimating()
            }else {
               self.homeStatusesArr = statusesList
            }
            
            //刷新列表
            self.tableView.reloadData()
        }
        
    }
    

    // MARK: - Table view data source
    //设置行数
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return homeStatusesArr.count ?? 0
    }

    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(homeCellID, forIndexPath: indexPath) as! HLHomeCell
        //当现实到最后一个cell时开始加载数据
        if !footerIndicatorView.isAnimating() && indexPath.row == homeStatusesArr.count - 1 {
            //底部视图开始动画
            footerIndicatorView.startAnimating()
            //加载数据
            loadHomeData()
        }
        //设置数据
        cell.homeStatus = homeStatusesArr[indexPath.row]
        return cell
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //MARK: 懒加载
    //底部指示视图
    private lazy var footerIndicatorView: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .Gray)
    //顶部自定义下拉刷新视图
    private lazy var headerRefreshView: HLHomeRefreshControl = HLHomeRefreshControl()

}
